package com.sxt.sys.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.sxt.sys.common.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.sys.domain.Dept;
import com.sxt.sys.domain.Role;
import com.sxt.sys.domain.User;
import com.sxt.sys.service.DeptService;
import com.sxt.sys.service.RoleService;
import com.sxt.sys.service.UserService;
import com.sxt.sys.vo.UserVo;

import cn.hutool.core.util.IdUtil;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 老雷
 * @since 2019-09-20
 */
@RestController
@RequestMapping("/p/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	//基本接口外露
	@HystrixCommand
	@RequestMapping("getOne")
	public User getOne(String tokengetPrincipal){
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("loginname", tokengetPrincipal);
		 return this.userService.getOne(queryWrapper);
	}

	
	/**
	 * 用户全查询
	 */
	@HystrixCommand
	@RequestMapping("loadAllUser")
	public DataGridView loadAllUser(UserVo userVo) {
		List<User> list = this.userService.loadAllUser(userVo);
		return new DataGridView((long) list.size(), list);
		
	}
	
	

	/**
	 * 加载最大的排序码
	 * @param
	 * @return
	 */
	@HystrixCommand
	@RequestMapping("loadUserMaxOrderNum")
	public Map<String,Object> loadUserMaxOrderNum(){
		Map<String, Object> map=new HashMap<String, Object>();
		List<User> list = this.userService.loadUserMaxOrderNum();
		if(list.size()>0) {
			map.put("value", list.get(0).getOrdernum()+1);
		}else {
			map.put("value", 1);
		}
		return map;
	}
	
	
	/**
	 * 根据部门ID查询用户
	 */
	@HystrixCommand
	@RequestMapping("loadUsersByDeptId")
	public DataGridView loadUsersByDeptId(Integer deptid) {
		List<User> list = this.userService.loadUsersByDeptId(deptid);
		return new DataGridView(list);
	}
	
	
	/**
	 * 把用户名转成拼音
	 */
	@HystrixCommand
	@RequestMapping("changeChineseToPinyin")
	public Map<String,Object> changeChineseToPinyin(String username){
		 Map<String,Object> map=new HashMap<>();
		 if(null!=username) {
			 map.put("value", PinyinUtils.getPingYin(username));
		 }else {
			 map.put("value", "");
		 }
		 return map;
	}
	
	/**
	 * 添加用户
	 */
	@HystrixCommand
	@RequestMapping("addUser")
	public ResultObj addUser(UserVo userVo) {
		if(this.userService.addUser(userVo)){
			return ResultObj.ADD_SUCCESS;
		} else {
			return ResultObj.ADD_ERROR;
		}
	}

	
	/**
	 * 根据用户ID查询一个用户
	 */
	@HystrixCommand
	@RequestMapping("loadUserById")
	public DataGridView loadUserById(Integer id) {
		return new DataGridView(this.userService.getById(id));
	}
	
	/**
	 * 修改用户
	 */
	@HystrixCommand
	@RequestMapping("updateUser")
	public ResultObj updateUser(UserVo userVo) {
		if(this.userService.updateUser(userVo)){
			return ResultObj.UPDATE_SUCCESS;
		} else  {
			return ResultObj.UPDATE_ERROR;
		}
	}

	/**
	 * 删除用户
	 */
	@HystrixCommand
	@RequestMapping("deleteUser")
	public ResultObj deleteUser(Integer id) {
		if(this.userService.deleteUser(id)){
			return ResultObj.DELETE_SUCCESS;
		} else {
			return ResultObj.DELETE_ERROR;
		}
	}
	/**
	 * 重置用户密码
	 */
	@HystrixCommand
	@RequestMapping("resetPwd")
	public ResultObj resetPwd(Integer id) {
		if(this.userService.resetPwd(id)){
			return ResultObj.RESET_SUCCESS;
		} else {
			return ResultObj.RESET_ERROR;
		}
	}
	
	
	/**
	 * 根据用户ID查询角色并选中已拥有的角色
	 */
	@HystrixCommand
	@RequestMapping("initRoleByUserId")
	public DataGridView initRoleByUserId(Integer id) {
		List<Map<String, Object>> listMaps = this.userService.initRoleByUserId(id);
		return new DataGridView(Long.valueOf(listMaps.size()), listMaps);
	}
	
	/**
	 * 保存用户和角色的关系
	 */
	@HystrixCommand
	@RequestMapping("saveUserRole")
	public ResultObj saveUserRole(Integer uid,Integer[] ids) {
		try {
			this.userService.saveUserRole(uid,ids);
			return ResultObj.DISPATCH_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.DISPATCH_ERROR;
		}
	}

	/**
	 * 用户修改密码-旧密码验证
	 */
	@HystrixCommand
	@RequestMapping("checkedoldpwd")
	public ResultObj checkedoldpwd(String oldpwd){
		System.out.println(oldpwd+"+++++++++++++++++++++++++++++++++++++22222");
		User user = (User) WebUtils.getSession().getAttribute("user");
		String oldpwdSalt = new Md5Hash(oldpwd, user.getSalt(), 2).toString();
		//判断旧密码是否一样
		if(!oldpwdSalt.equals(user.getPwd())){
			return ResultObj.OPERATE_ERROR;
		} else {
			return ResultObj.OPERATE_SUCCESS;
		}
	}
	/**
	 * 用户修改密码
	 */
	@HystrixCommand
	@RequestMapping("udpatepwd")
	public ResultObj udpatepwd(String newpwd){
		if (this.userService.udpatepwd(newpwd)){
			return ResultObj.RESETPWD_SUCCESS;
		} else {
			return ResultObj.RESETPWD_ERROR;
		}
	}


}

